package useTask.hbaseTask;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;
import java.util.List;

public class getHbase {
    static Configuration conf;
    static Connection HConn;
    static Table table;
    static Get get;

    public static void getData(String tableName,String rowKey) throws IOException {
        conf = HBaseConfiguration.create();
        HConn = ConnectionFactory.createConnection(conf);
        table = HConn.getTable(TableName.valueOf(tableName));

        try{
            get = new Get(rowKey.getBytes());
            Result result = table.get(get);
            List<Cell> cells = result.listCells();
            for (Cell cell : cells) {
                String row_key = Bytes.toString( CellUtil.cloneRow(cell) );
                String Family = Bytes.toString( CellUtil.cloneFamily(cell) );
                String column = Bytes.toString( CellUtil.cloneQualifier(cell) );
                String value = Bytes.toString( CellUtil.cloneValue(cell) );

                System.out.printf("%s表通过行键 <%s> 查询的结果如下 >> ",tableName,rowKey);
                System.out.println(row_key+" "+Family+" "+column+" "+value);
            }

        }finally {
            table.close();
            HConn.close();
        }
    }

    public static void getData(String tableName,String rowKey,String family) throws IOException {
        conf = HBaseConfiguration.create();
        HConn = ConnectionFactory.createConnection(conf);
        table = HConn.getTable(TableName.valueOf(tableName));

        try{
            get = new Get(rowKey.getBytes());
            get.addFamily(family.getBytes());
            Result result = table.get(get);
            List<Cell> cells = result.listCells();
            for (Cell cell : cells) {
                String row_key = Bytes.toString( CellUtil.cloneRow(cell) );
                String Family = Bytes.toString( CellUtil.cloneFamily(cell) );
                String column = Bytes.toString( CellUtil.cloneQualifier(cell) );
                String value = Bytes.toString( CellUtil.cloneValue(cell) );

                System.out.printf("%s表通过行键 <%s> 和列族 <%s> 查询的结果如下 >> ",tableName,rowKey,family);
                System.out.println(row_key+" "+Family+" "+column+" "+value);
            }

        }finally {
            table.close();
            HConn.close();
        }
    }

    public static void getData(String tableName,String rowKey,String family,String Column) throws IOException {
        conf = HBaseConfiguration.create();
        HConn = ConnectionFactory.createConnection(conf);
        table = HConn.getTable(TableName.valueOf(tableName));

        try{
            get = new Get(rowKey.getBytes());
            get.addColumn(family.getBytes(),Column.getBytes());
            Result result = table.get(get);
            List<Cell> cells = result.listCells();
            for (Cell cell : cells) {
                String row_key = Bytes.toString( CellUtil.cloneRow(cell) );
                String Family = Bytes.toString( CellUtil.cloneFamily(cell) );
                String column = Bytes.toString( CellUtil.cloneQualifier(cell) );
                String value = Bytes.toString( CellUtil.cloneValue(cell) );

                System.out.printf("%s表通过行键 <%s> 和列族 <%s> 和列名 <%s> 查询的结果如下 >> ",tableName,rowKey,family,Column);
                System.out.println(row_key+" "+Family+" "+column+" "+value);
            }

        }finally {
            table.close();
            HConn.close();
        }
    }

}
